<?php
namespace app\system\model;
use \think\Model;
use think\Db;

class WebSet extends Base{
    /***
     * 配置保存
     * @param array $data
     * @return int|string
     * @throws \think\Exception
     */
    public function save_set($data=[]){
        dump($data);
        try {
            $ok = false;
            $data['en_name'] = $data['en_name']??'';
            $data['logo'] = $data['logo']??'';
            $data['mobile_logo'] = $data['mobile_logo']??'';
            $data['url'] = $data['url']??'';
            $data['yqx_url'] = $data['yqx_url']??'';
            $data['address'] = $data['address']??'';
            $data['realname'] = $data['realname']??'';
            $data['qrcode'] = $data['qrcode']??'';
            $data['tel'] = $data['tel']??'';
            $data['fax'] = $data['fax']??'';
            $data['icp'] = $data['icp']??'';
            $data['is_close'] = $data['is_close']??'';
            $data['close_reason'] = $data['close_reason']??'';
            $data['code'] = $data['code']??'';
            $data['logo_bottom'] = $data['logo_bottom']??'';

            $where['web_id'] = $data['web_id'];
            $where['lang_id'] = $data['lang_id'];
            $id = Db::connect(config('database.db_config2'))->name('WebSet')->where($where)->value('id');
            if ( isset( $id ) ) {
                $ok = Db::connect(config('database.db_config2'))->name('WebSet')->where(["id"=>$id])->update($data);
            } else {
                $ok = Db::connect(config('database.db_config2'))->name('WebSet')->insert($data);
            }
            if($ok !== false){
                //更新缓存
                web_set_list_cache();
            }
            return $ok;

        }catch(\Exception $e){
            return false;
        }
    }


    //获取网站配置
    public function get_set($data = []){
        $where['web_id'] = $data['web_id'];
        $where['lang_id'] = $data['lang_id'];
        try{
            $web_set_list = take_cache('web_set_list');
            foreach ($web_set_list as $k=>$v){
                if($v['web_id'] == $data['web_id'] && $v['lang_id'] == $data['lang_id']){
                    $web_set = $v;
                }
            }
            if(!isset($web_set)){
                $web_set = Db::connect(config('database.db_config2'))->name('WebSet')->where($where)->find();
            }
            return $web_set;
        }catch(Exception $e){
            return false;
        }
    }

    /***
     * 网站配置列表
     * @param $where
     * @param string $field
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function web_set_list($where=1,$param=[],$field="*",$name=""){
        try {
            //$list['total'] = Db::connect(config('database.db_config2'))->name('WebSet')->where($where)->count();
            $web_set_list = Db::connect(config('database.db_config2'))->name('WebSet')->where($where)->field($field)->order($param['order_str'])->select();
            $new_list = $list_cache = [];
            if (!empty($web_set_list)) {
                foreach ($web_set_list as $key => $value) {
                    $new_list[$value['id']] = $value;
                    if (!empty($name)) $list_cache[$value['web_id']] = $value;
                }
            }
            //$list['list'] = !empty($list_cache) ? $list_cache : $new_list;
            $list = !empty($list_cache) ? $list_cache : $new_list;
            unset($list_cache);
            return $list;
        }catch(\Exception $e){
            return false;
        }

    }
    public function system_set($data=[]){
        $info = [];
        //根据端口id,获取选中模板的has编码
        $info['snapshot_hash'] = Db::name('templet_hash')->where(['web_id'=>$data['web_id'],'client_id'=>$data['client_id'],'is_choice'=>1])->order('addtime desc')->value('hash');
//        $rsa_key = take_cache('rsa_key',false);
        $info['private_key'] = '';//暂时没用,先不返还
        $info['mall_domain'] = \app\common\model\Base::get_domain_set('mall_domain',1);
        $info['college_domain'] = $info['mall_domain'].'/yali_college';//鸭梨学院
        $info['forum_domain'] = $info['mall_domain'].'/front/forum';//鸭梨社区
        $info['mini_program_domain'] = $info['mall_domain'].'/small';//小程序
        $info['qiniu_domain'] = config('qiniu.DOMAIN');
        $info['is_first_login'] = db('web',config('database.db_config2'),false)->where(['id'=>$data['web_id']])->value('is_first_login');
        /****引导页标识*****/
        if(!empty($info['is_first_login'])){
            $info['is_first_login'] =  explode(',',$info['is_first_login']);
        }else{
            $info['is_first_login'] = [];
        }
        //检测账号是不是超级管理员
        $info['super_admin'] = \app\system\model\SuperAdmin::check_super_admin($data['web_id']);
        return $info;
    }

    public function save_boot_page($data = []){
        $res = db('web',config('database.db_config2'),false)->where(['id'=>$data['web_id']])->update(['is_first_login'=>$data['is_first_login']]);
        if($res){
            return true;
        }else{
            return false;
        }
    }
    /**
     * @param array $data
     * @return bool|int|string
     * 修改站点信息
     */
    public function edit_website($data=[]){
        //修改小程序站点信息
        try{
            $ok = false;
            $web_set = \app\common\model\Base::get_domain_set();
            $data['logo'] = !empty($data['logo'])?str_replace($web_set['qn_domain'],'',$data['logo']):'';
            $ok = Db::connect(config('database.db_config2'))->name('web_set')->where(['web_id'=>$data['web_id'],'lang_id'=>$data['lang_id']])->update(['name'=>$data['name'],'logo'=>$data['logo'],'descript'=>$data['descript']]);
            if($ok !== false){
                //更新缓存信息
                $web = \app\system\model\Login::get_login_token($data['token']);
                if(!empty($web['web_list'][$data['web_id']])){
                    $web['web_list'][$data['web_id']]['name'] = $data['name']??'';
                    $web['web_list'][$data['web_id']]['logo'] =  $web['web_list'][$data['web_id']]['head_photo'] =  !empty($data['logo'])?$web_set['qn_domain'].$data['logo']:'';
                    $web['web_list'][$data['web_id']]['descript'] = $data['descript']??'';
                    $web = \app\system\model\Login::update_login_token($data['token'],$web);
                }
            }
            return $ok;
        }catch(\Exception $e){
            return false;
        }
    }
}